import type { Router } from "vue-router";
import { AxiosCanceler } from "@/utils/http/axiosCancel";
import { getConfig } from "@/config";
import { createPermissionGuard } from "./permissionGuard";

export function setupRouterGuard(router: Router) {
  createHttpGuard(router);
  createPermissionGuard(router);
}

function createHttpGuard(router: Router) {
  const { RemoveHttpBeforEach } = getConfig();
  let axiosCanceler: AxiosCanceler | null;
  if (RemoveHttpBeforEach) {
    axiosCanceler = new AxiosCanceler();
  }
  router.beforeEach(async () => {
    // 跳转时移除所有 pending 请求
    axiosCanceler?.removeAllPending();
    return true;
  });
}
